package cn.virens.common.mybatis.dynamic_table.proxy;

import cn.virens.common.mybatis.dynamic_table.DynamicTableNameFactory;
import cn.virens.common.util.exception.APIException;

import java.lang.reflect.Method;

public class DynamicTableNameProxy {
    /**
     * 生成动态表名占位内容
     *
     * @param table 默认表名
     * @return
     * @throws APIException
     */
    protected static String generateDynamicTableName(String table) throws APIException {
        return "${@" + DynamicTableNameFactory.class.getName() + "@generate('" + table + "')}";
    }

    /**
     * 检查函数是否为指定函数
     */
    protected static boolean isMethod(Method method, String name, Class<?> rt, Class<?>... pt) {
        if (name != null && method != null && name.equals(method.getName())) {
            // 对请求函数参数进行效验--------------------------------------
            var ptArray = method.getParameterTypes();
            if (pt != null && pt.length != ptArray.length) {
                return false;
            }
            if (pt == null && ptArray.length > 0) {
                return false;
            }
            for (int i = 0; i < ptArray.length; i++) {
                if (ptArray[i] != pt[i]) {
                    return false;
                }
            }

            // 检查函数返回值类型是否一致----------------------------------
            var rtVal = method.getReturnType();
            if (rt == null || rt == void.class) {
                if (rtVal != void.class) {
                    return false;
                }
            } else if (rt != rtVal) {
                return false;
            }
        } else {
            return false;
        }

        return true;
    }
}
